package com.example.musicplayer.adapter;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import androidx.recyclerview.widget.RecyclerView;

import com.example.musicplayer.R;
import com.example.musicplayer.activity.MusicActivity;
import com.example.musicplayer.bean.Album;
import com.example.musicplayer.fragment.MainTabFindFragment;
import com.example.musicplayer.fragment.MainTabMyFragment;

import java.io.Serializable;
import java.util.List;

public  class RecommendListAdapter extends RecyclerView.Adapter<RecommendListAdapter.ViewHolder> {
    private List<Album> list;
    private Context context;

    public RecommendListAdapter(MainTabFindFragment mainTabFindFragment, List<Album> dataList) {
        this.context = mainTabFindFragment.getContext();
        this.list = dataList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_tab_recommend_rv, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Album content = list.get(position);
        holder.imageView.setImageBitmap(content.getAlbumCover());
        holder.name.setText(content.getName());

        Bitmap finalBitmap = (list.get(position).getAlbumCover());
        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), finalBitmap); //创建RoundedBitmapDrawable对象
        roundedBitmapDrawable.setCornerRadius(100); //设置圆角Radius（根据实际需求）
        roundedBitmapDrawable.setAntiAlias(true); //设置抗锯齿
        holder.imageView.setImageDrawable(roundedBitmapDrawable);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;//封面
        TextView name;//歌单名
        LinearLayout ll_item_fragment_layout;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_item_fragment_find);
            name = itemView.findViewById(R.id.tv_item_fragment_find);
            ll_item_fragment_layout = itemView.findViewById(R.id.ll_item_fragment_layout);
            ll_item_fragment_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    System.out.println("position:"+position);
                    Intent intent = new Intent(itemView.getContext(), MusicActivity.class);
                    intent.putExtra("album",(Serializable)list.get(position));
                    intent.putExtra("isWEBAlbum",true);
                    itemView.getContext().startActivity(intent);
                }
            });
        }
    }
}